package org.lagou.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lagou.Dao.CompanyDao;
import org.lagou.Dao.IssueDao;
import org.lagou.Dao.UserDao;
import org.lagou.Dao.impl.CompanyDaoImpl;
import org.lagou.Dao.impl.IssueDaoImpl;
import org.lagou.Dao.impl.UserDaoImpl;
import org.lagou.bean.CompanyBean;
import org.lagou.bean.IssueBean;
import org.lagou.bean.ReponseBody;
import org.lagou.bean.UserBean;
import org.lagou.tools.Tools;

import com.google.gson.Gson;

@WebServlet("/lssueServlet")
public class lssueServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	IssueDao iss = new IssueDaoImpl();
	Gson gson = new Gson();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		String type = request.getParameter("type");
		switch (type) {
		case "getIssueAndCompany":
			getIssueAndCompany(request, response);
			break;
		case "getIssueAll":
			getIssueAll(request, response);
			break;
		case "getIssuePagin":
			getIssuePagin(request, response);
			break;
		case "addIssue":
			addIssue(request, response);
			break;
		case "updateIssue":
			updateIssue(request, response);
			break;
		case "deleteIssueById":
			deleteIssueById(request, response);
			break;
		case "getStudentCount":
			getStudentCount(request, response);
			break;
		case "updatedel":
			updatedel(request, response);
			break;
		case "SelectIssueAndCompanyByName":
			SelectIssueAndCompanyByName(request, response);
			break;
		case "SelectIssueAndCompanyByID":
			SelectIssueAndCompanyByID(request, response);
			break;
		case "getIssueByCompanyid":
			getIssueByCompanyid(request, response);
			break;	
		default:
			break;
		}

	}

	private void SelectIssueAndCompanyByID(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		int id = Integer.parseInt(request.getParameter("id"));

		rb.setData(iss.SelectIssueAndCompanyByID(id));

		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void getIssueByCompanyid(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		List<UserBean> ulist = udao.getByEmailUser(email);
		int userid = 0;
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		CompanyDao cdao = new CompanyDaoImpl();
		List<CompanyBean> clist=cdao.getCompanyByUserid(userid);
		int companyid=-1;
		for (CompanyBean c : clist) {
			companyid=c.getId();
		}

		if (!iss.getIssueByCompanyid(companyid).isEmpty()) {
			rb.setMsg("查询成功");
			rb.setData(iss.getIssueByCompanyid(companyid));
		} else {
			rb.setMsg("查询失败");
			rb.setStatus(0);
		}

		try {
			response.getWriter().write(new Gson().toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	private void SelectIssueAndCompanyByName(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();
		String name = request.getParameter("name");
		rb.setData(iss.SelectIssueAndCompanyByName(name));
		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void updatedel(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();
		int id = Integer.parseInt(request.getParameter("id"));

		rb.setData(iss.updatedel(id));

		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private void getIssueAndCompany(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();
		rb.setData(iss.getIssueAndCompany());
		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void getStudentCount(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();
//		rb.setData(iss.getStudentCount());
		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

//	private void updateIssue(HttpServletRequest request, HttpServletResponse response) {
//		ReponseBody rb=new ReponseBody();
//		
//		//��������в�����Ҫ��֤
//		int id=Integer.parseInt(request.getParameter("is"));
//		int companyid=Integer.parseInt(request.getParameter("companyid"));
//		String name=request.getParameter("name");
//		String category=request.getParameter("category");
//		String dept=request.getParameter("dept");
//		String nature=request.getParameter("nature");
//		//����û����ݵ�price����Ϊ�ա��˴����׳�null�쳣����Ҫ�ж���
//		int minsalary=Integer.parseInt(request.getParameter("minsalary"));
//		int maxsalary=Integer.parseInt(request.getParameter("maxsalary"));
//		String city=request.getParameter("city");
//		String experience=request.getParameter("experience");
//		String education=request.getParameter("education");
//		String temptation=request.getParameter("temptation");
//		String summary=request.getParameter("summary");
//		String address=request.getParameter("address");
//		String email=request.getParameter("email");
//		String companyname=request.getParameter("companyname");
//		String date=request.getParameter("date");
//		int del=Integer.parseInt(request.getParameter("del"));
//		
//		IssueBean info=new IssueBean(id, companyid, name, category, dept, nature, minsalary, maxsalary, city, experience, education, temptation, summary, address, email, companyname, date, del);
//		rb.setData(iss.updateIssue(info));
//		
//		try {
//			response.getWriter().write(gson.toJson(rb));
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//	}

//	private void updateIssue(HttpServletRequest request, HttpServletResponse response) {
//ReponseBody rb=new ReponseBody();
//		
//		//��������в�����Ҫ��֤
//		int id=Integer.parseInt(request.getParameter("is"));
//		int companyid=Integer.parseInt(request.getParameter("companyid"));
//		String name=request.getParameter("name");
//		String category=request.getParameter("category");
//		String dept=request.getParameter("dept");
//		String nature=request.getParameter("nature");
//		//����û����ݵ�price����Ϊ�ա��˴����׳�null�쳣����Ҫ�ж���
//		int minsalary=Integer.parseInt(request.getParameter("minsalary"));
//		int maxsalary=Integer.parseInt(request.getParameter("maxsalary"));
//		String city=request.getParameter("city");
//		String experience=request.getParameter("experience");
//		String education=request.getParameter("education");
//		String temptation=request.getParameter("temptation");
//		String summary=request.getParameter("summary");
//		String address=request.getParameter("address");
//		String email=request.getParameter("email");
//		String companyname=request.getParameter("companyname");
//		String date=request.getParameter("date");
//		int del=Integer.parseInt(request.getParameter("del"));
//		String scale=request.getParameter("scale");
//		String stage=request.getParameter("stage");
//		
//		IssueBean info=new IssueBean(id, companyid, name, category, dept, nature, minsalary, maxsalary, city, experience, education, temptation, summary, address, email, companyname, date, del, scale, stage);
//		rb.setData(iss.updateIssue(info));
//		
//		try {
//			response.getWriter().write(gson.toJson(rb));
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//	}

	private void deleteIssueById(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();
		int id = Integer.parseInt(request.getParameter("id"));
		rb.setData(iss.deleteIssueById(id));

		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void updateIssue(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		int id = Integer.parseInt(request.getParameter("is"));
		int companyid = Integer.parseInt(request.getParameter("companyid"));
		String name = request.getParameter("name");
		String category = request.getParameter("category");
		String dept = request.getParameter("dept");
		String nature = request.getParameter("nature");
		int minsalary = Integer.parseInt(request.getParameter("minsalary"));
		int maxsalary = Integer.parseInt(request.getParameter("maxsalary"));
		String city = request.getParameter("city");
		String experience = request.getParameter("experience");
		String education = request.getParameter("education");
		String temptation = request.getParameter("temptation");
		String summary = request.getParameter("summary");
		String address = request.getParameter("address");
		String email = request.getParameter("email");
		String companyname = request.getParameter("companyname");
		String date = Tools.getNowDate();
		int del = Integer.parseInt(request.getParameter("del"));

		IssueBean info = new IssueBean(id, companyid, name, category, dept, nature, minsalary, maxsalary, city,
				experience, education, temptation, summary, address, email, companyname, date, del);
		rb.setData(iss.updateIssue(info));

		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void addIssue(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		int userid = 0;
		List<UserBean> ulist = udao.getByEmailUser(email);
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		CompanyDao cdao = new CompanyDaoImpl();
		List<CompanyBean> clist = cdao.getCompanyByUserid(userid);
		int companyid = -1;
		String companyname = null;
		for (CompanyBean c : clist) {
			companyid = c.getId();
			companyname = c.getName();
		}

		String name = request.getParameter("positionName");
		String category = request.getParameter("positionType");
		String dept = request.getParameter("department");
		String nature = request.getParameter("jobNature");
		int minsalary = Integer.parseInt(request.getParameter("salaryMin"));
		int maxsalary = Integer.parseInt(request.getParameter("salaryMax"));
		String city = request.getParameter("workAddress");
		String experience = request.getParameter("workYear");
		String education = request.getParameter("education");
		String temptation = request.getParameter("positionAdvantage");
		String summary = request.getParameter("positionDetail");
		String address = request.getParameter("positionAddress");
		String newemail = request.getParameter("email");
		String date = Tools.getNowDate();
		int del = 0;

		IssueBean info = new IssueBean(companyid, name, category, dept, nature, minsalary, maxsalary, city, experience,
				education, temptation, summary, address, newemail, companyname, date, del);
		rb.setData(iss.addIssue(info));

		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void getIssuePagin(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		int pageIndex = request.getParameter("pageIndex") == null ? 1
				: Integer.parseInt(request.getParameter("pageIndex"));
		int pageSize = request.getParameter("PageSize") == null ? 1
				: Integer.parseInt(request.getParameter("PageSize"));

		rb.setData(iss.getIssuePagin(pageIndex, pageSize));

		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void getIssueAll(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();
		rb.setData(iss.getIssueAll());
		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
